package com.dd.admin.controller.system;

import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.common.aspect.annotation.LogOperation;
import com.dd.common.convert.SysRoleConvert;
import com.dd.common.dto.system.menu.SysMenuDTO;
import com.dd.common.dto.system.role.SysRoleDTO;
import com.dd.common.dto.system.role.SysRoleQueryDTO;
import com.dd.common.entity.SysRole;
import com.dd.common.pojo.dto.IdsDTO;
import com.dd.common.pojo.dto.PageDTO;
import com.dd.common.pojo.dto.group.CreateGroup;
import com.dd.common.pojo.dto.group.UpdateGroup;
import com.dd.common.service.SysRoleService;
import com.dd.common.utils.AjaxResult;
import com.dd.common.utils.PageResult;
import com.dd.common.validator.annotation.IDMust;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author cheng
 */
@SaIgnore
@Tag(name = "角色管理")
@Validated
@RequestMapping("/sys/role")
@RestController
public class SysRoleController {
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysRoleConvert sysRoleConvert;

    @LogOperation(title = "角色新增")
    @Operation(summary = "角色新增")
    @io.swagger.v3.oas.annotations.parameters.RequestBody(
            description = "菜单数据",
            required = true,
            content = @Content(
                    mediaType = MediaType.APPLICATION_JSON_VALUE,
                    schema = @Schema(implementation = SysRoleDTO.class)
            )
    )
    @PostMapping("/add")
    public AjaxResult<?> add(@Validated(CreateGroup.class) @RequestBody SysRoleDTO dto) {
        return AjaxResult.success(sysRoleService.save(sysRoleConvert.dtoToEntity(dto)));
    }

    @Operation(summary = "角色编辑", requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
            description = "菜单数据",
            content = @Content(
                    mediaType = MediaType.APPLICATION_JSON_VALUE,
                    schema = @Schema(implementation = SysRoleDTO.class)
            )
    ))
    @PatchMapping("/edit")
    public AjaxResult<?> edit(@Validated(UpdateGroup.class) SysRoleDTO dto) {
        return AjaxResult.success(sysRoleService.updateById(sysRoleConvert.dtoToEntity(dto)));
    }

    @Operation(summary = "角色列表")
    @GetMapping("/list")
    public AjaxResult<?> list(@Validated SysRoleQueryDTO dto) {
        return AjaxResult.success(sysRoleService.list(new LambdaQueryWrapper<SysRole>()
                .eq(dto.getStatus() != null, SysRole::getStatus, dto.getStatus())
                .like(StringUtils.hasText(dto.getRoleName()), SysRole::getRoleName, dto.getRoleName())
                .like(StringUtils.hasText(dto.getRoleCode()), SysRole::getRoleCode, dto.getRoleCode())
                .ge(dto.getCreatedAtStart() != null, SysRole::getCreateTime, dto.getCreatedAtStart())
                .le(dto.getCreatedAtEnd() != null, SysRole::getCreateTime, dto.getCreatedAtEnd())
                .ge(dto.getUpdatedAtStart() != null, SysRole::getUpdateTime, dto.getUpdatedAtStart())
                .le(dto.getUpdatedAtEnd() != null, SysRole::getUpdateTime, dto.getUpdatedAtEnd())
                .orderByAsc(SysRole::getSort)
        ));
    }

    @Operation(summary = "角色分页列表")
    @GetMapping("/page")
    public AjaxResult<?> pageQuery(@Validated PageDTO pageDTO, @Validated SysRoleQueryDTO dto) {
        Page<SysRole> page = Page.of(pageDTO.getCurrent(), pageDTO.getSize());
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dto.getStatus() != null, SysRole::getStatus, dto.getStatus())
                .like(StringUtils.hasText(dto.getRoleName()), SysRole::getRoleName, dto.getRoleName())
                .like(StringUtils.hasText(dto.getRoleCode()), SysRole::getRoleCode, dto.getRoleCode())
                .ge(dto.getCreatedAtStart() != null, SysRole::getCreateTime, dto.getCreatedAtStart())
                .le(dto.getCreatedAtEnd() != null, SysRole::getCreateTime, dto.getCreatedAtEnd())
                .ge(dto.getUpdatedAtStart() != null, SysRole::getUpdateTime, dto.getUpdatedAtStart())
                .le(dto.getUpdatedAtEnd() != null, SysRole::getUpdateTime, dto.getUpdatedAtEnd())
                .orderByAsc(SysRole::getSort);

        IPage<SysRole> result = sysRoleService.page(page, queryWrapper);
        return AjaxResult.success(PageResult.iPageHandle(result.getTotal(), result.getCurrent(), result.getSize(), result.getRecords()));
    }

    @Operation(summary = "角色删除", parameters = @Parameter(name = "id", in = ParameterIn.PATH, description = "角色ID"))
    @DeleteMapping("/delete/{id}")
    public AjaxResult<?> delete(@PathVariable @IDMust Long id) {
        return AjaxResult.success(sysRoleService.removeById(id));
    }

    @Operation(summary = "角色权限", description = "角色菜单树", parameters = @Parameter(name = "roleId", in = ParameterIn.PATH, description = "角色ID"))
    @GetMapping("/menu/{roleId}")
    public AjaxResult<?> roleMenu(@PathVariable @IDMust Long roleId){
        return AjaxResult.success(sysRoleService.getRoleMenu(roleId));
    }
    @Operation(summary = "菜单分配", description = "角色菜单分配", parameters = @Parameter(name = "roleId", in = ParameterIn.PATH, description = "角色ID"))
    @PostMapping("/menu/assign/{roleId}")
    public AjaxResult<?> assignMenu(@PathVariable @IDMust Long roleId, @Validated @RequestBody IdsDTO dto){
        return AjaxResult.success(sysRoleService.assignMenu(roleId, dto.getIds()));
    }


}
